package com.zyuan.lawyerport.view.dModule.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zyuan.lawyerport.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.tool.Util;

/**
 * 作者 ：RAN
 * 时间 ：2019/10/11 17:43
 * 类名 ：PersonalInfoActivity
 * 描述 ：个人信息
 * 版本 ：1.0
 */
public class PersonalInfoActivity extends BaseActivity implements View.OnClickListener, PopupWindow.OnDismissListener {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.ll_head)
    LinearLayout llHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_name)
    LinearLayout llName;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.ll_sex)
    LinearLayout llSex;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;


    @Override
    public void onDismiss() {
        setBackgroundAlpha(1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pop_text_pick_phone_album:
//                ToastUtils.show("从手机相册选择");
                popupWindow.dismiss();
                break;
            case R.id.pop_text_pick_camera:
//                ToastUtils.show("打开相机拍照");
                popupWindow.dismiss();
                break;
            case R.id.pop_text_cancel:
                popupWindow.dismiss();
                break;
        }
    }

    @Override
    public void init() {
        tvTitle.setText("个人信息");
    }

    @Override
    public int setLayout() {
        return R.layout.activity_personal_info;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {

    }


    private PopupWindow popupWindow;
    private View popupView;
    private TranslateAnimation animation;


    private void showPop() {
        setBackgroundAlpha(0.2f);
        popupView = View.inflate(activity, R.layout.customize_popupwindow_layout, null);
        popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);


        animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, -1, Animation.RELATIVE_TO_PARENT, 0);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(200);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1);
            }
        });
        setOnPopupViewClick(popupView);

        popupWindow.showAtLocation(tvTitle, Gravity.BOTTOM, 0, Util.dip2Px(activity, 75));
        popupView.startAnimation(animation);

    }

    /**
     * 设置屏幕背景透明效果
     *
     * @param alpha
     */
    public void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = alpha;
        getWindow().setAttributes(lp);

    }

    private void setOnPopupViewClick(View view) {
        TextView mPopTextPickPhoneAlbum;
        TextView mPopTextPickCamera;
        TextView mPopTextCancel;

        mPopTextPickPhoneAlbum = (TextView) view.findViewById(R.id.pop_text_pick_phone_album);
        mPopTextPickCamera = (TextView) view.findViewById(R.id.pop_text_pick_camera);
        mPopTextCancel = (TextView) view.findViewById(R.id.pop_text_cancel);
        mPopTextPickPhoneAlbum.setOnClickListener(this);
        mPopTextPickCamera.setOnClickListener(this);
        mPopTextCancel.setOnClickListener(this);

    }


    @OnClick({R.id.ll_back, R.id.ll_right, R.id.ll_head, R.id.ll_name, R.id.ll_sex, R.id.ll_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_right:
                break;
            case R.id.ll_head: //打开弹框
                showPop();
                break;
            case R.id.ll_name:
                startActivity(new Intent(this, SetUserNameActivity.class));
                break;
            case R.id.ll_sex:
                startActivity(new Intent(this, SetGenderActivity.class));
                break;
            case R.id.ll_phone:
                startActivity(new Intent(this, SetPhoneActivity.class));
                break;
        }
    }
}
